package cn.wolfcode.crm.web.controller;


import cn.wolfcode.crm.query.ClaimReportQueryObject;
import cn.wolfcode.crm.service.IClaimReportService;
import cn.wolfcode.crm.util.PermissionName;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("claimReport")
public class ClaimReportController {

    @Autowired
    private IClaimReportService claimReportService;

    /**
     * 索赔报表页面
     * @return
     */
    @RequestMapping("")
    public String claimReportPage(){
        return "reportforms/claim/claimReport";
    }

    /**
     * 索赔报表页面数据
     * @param qo
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    @PermissionName("理赔报表数据")
    public List<Map<String, Object>> list(ClaimReportQueryObject qo) {
        List<Map<String, Object>> list = claimReportService.queryClaimReportList(qo);
        return list;
    }

    /**
     * 索赔柱状图数据
     * @return 柱状图
     */
    @RequestMapping("claimbar")
    public String claimbar(ClaimReportQueryObject qo, Model model){
        //分组类型, 获取到qo的分组字段, 用作副标题*/
        model.addAttribute("groupClaimByTypes",
                ClaimReportQueryObject.groupSaleByTypes.get(qo.getGroupByType()));

        //System.out.println(ClaimReportQueryObject.groupSaleByTypes.get(qo.getGroupByType()));
        //System.out.println(ClaimReportQueryObject.groupSaleByTypes);
        //System.out.println(qo.getGroupByType());

        List<Map<String, Object>> claimList = claimReportService.queryClaimReportList(qo);

        List<Object> goupByTypes = new ArrayList<>();
        List<Object> claimAmounts = new ArrayList<>();

        for (Map<String, Object> map : claimList) {
            goupByTypes.add(map.get("groupByType"));
            claimAmounts.add(map.get("totalAmount"));
        }
        model.addAttribute("groupByType", JSON.toJSONString(goupByTypes));
        model.addAttribute("totalAmount",JSON.toJSONString(claimAmounts));

        return "reportforms/claim/claimbar";
    }

    /**
     * 索赔饼状图数据
     * @return 饼状图
     */
    @RequestMapping("claimpie")
    public String claimpie(ClaimReportQueryObject qo, Model model){
        //分组类型, 获取到qo的分组字段, 用作副标题*/
        model.addAttribute("groupSaleByTypes", ClaimReportQueryObject
                .groupSaleByTypes.get(qo.getGroupByType()));

        /*获取到查询出来的结果集*/
        List<Map<String, Object>> claimReportList = claimReportService.queryClaimReportList(qo);

        List<Object> goupByTypes = new ArrayList<>();
        List<Map<String, Object>> datas = new ArrayList<>();

        /*循环遍历结果集*/
        for (Map<String, Object> map : claimReportList) {
            goupByTypes.add(map.get("groupByType"));

            Map<String,Object> data = new LinkedHashMap<>();
            data.put("value", map.get("totalAmount"));
            data.put("name", map.get("groupByType"));
            datas.add(data);
        }
        model.addAttribute("goupByTypes", JSON.toJSONString(goupByTypes));
        model.addAttribute("datas",JSON.toJSONString(datas));

        return "reportforms/claim/claimpie";
    }

}
